package code.structure.module.user.data
{
	import code.structure.module.configs.ConfigModel;
	import code.untils.CommandNameConst;
	import code.untils.GameConst;
	import code.untils.MoneyTypeConst;
	
	import com.patience.mvc.context.Command;

	/**
	 *用户数据 
	 * @author liangqian
	 * 
	 */	
	public class UserData
	{
		//发送命令函数
		private var _sendCMD:Function;
		
		private var _userBaseData:UserBaseData;
		private var _configModel:ConfigModel;
		/** 是否达到游戏最大经验 */
		public var isMaxExp:Boolean;
		public function UserData(cmd:Function,configModel:ConfigModel)
		{
			_sendCMD = cmd;
			_configModel = configModel;
		}
		
		/**
		 * 解析数据
		 * @param	data
		 */
		public function analysisData(data:Object):void 
		{
			_userBaseData = new UserBaseData(data,_sendCMD);
		}
		
		/**
		 * 发送事件
		 * @param	name
		 * @param	param
		 * @param	target
		 */
		private function sendCommand(name:String, param:int,target:int = 0):void {
			var _command:Command = new Command(name);
			_command.data = param;
			_command.target = target;
			_sendCMD(_command);
		}
		
		/**
		 * 更新货币数量
		 * @param number 加多少
		 * @param type 哪一种货币类型
		 * @param isPop 当货币数量不够时是否弹窗提示 默认弹窗
		 * @param isSubtract 当货币数量足够时，是否扣除，默认扣除
		 * @return 返回货币是否足够 false 不够 true 够
		 * 
		 */		
		public function changeMoney(number:Number, type:int = 1, isPop:Boolean = true,isSubtract:Boolean = true):Boolean
		{
			var _cmd:Command;
			var _money:int;
			switch (type)
			{
				case MoneyTypeConst.MONEY_TYPE_COIN: 
					_money = _userBaseData.uCoin + number;
					if (_money < 0) 
						_cmd = new Command(CommandNameConst.COMMAND_NOT_ENOUGH_MONEY, MoneyTypeConst.MONEY_TYPE_COIN);
					else{
						if(isSubtract)changeCoins(number);
					}
					break;
				case MoneyTypeConst.MONEY_TYPE_DIAMOND: 
					_money = _userBaseData.uDiamond + number;
					if (_money < 0)
						_cmd = new Command(CommandNameConst.COMMAND_NOT_ENOUGH_MONEY, MoneyTypeConst.MONEY_TYPE_DIAMOND);
					else{
						if(isSubtract)changeDiamond(number);
					}
					break;
				case MoneyTypeConst.MONEY_TYPE_MAGIC: 
					_money = _userBaseData.uMagic + number;
					if (_money < 0)
						_cmd = new Command(CommandNameConst.COMMAND_NOT_ENOUGH_MONEY, MoneyTypeConst.MONEY_TYPE_MAGIC);
					else{
						if(isSubtract)changeMagic(number);
					}
					break;
			}
			if (_cmd && isPop)
				_sendCMD(_cmd);
			return (_cmd == null);
		}
		
		/**
		 * 改变等级
		 * @param	value
		 */
		public function changeLevel(value:int = 1):void
		{
			if(userBaseData.uLevel >= GameConst.MAX_LEVEL -1) return;
			userBaseData.uLevel += value;
			userBaseData.levelConfigData = _configModel.analysisLevel[userBaseData.uLevel];
			levelHelpHandler();
		}
		
		/**
		 * 改变钻石值
		 * @param	value
		 */
		public function changeDiamond(value:int):void
		{
			userBaseData.uDiamond += value;
			if(value < 0)
			{
				sendCommand(CommandNameConst.COMMAND_EXPENSE_CASH,Math.abs(value));
			}
		}
		
		/**
		 * 改变经验值
		 * @param	value
		 */
		public function changeExp(value:int):void
		{
			if (userBaseData.uExp >= userBaseData.levelConfigData.endexp) {
				isMaxExp = true
				return;
			}
			userBaseData.uExp += value;
			if (userBaseData.levelConfigData.endexp != 0 && userBaseData.uExp >= userBaseData.levelConfigData.endexp) {
				changeLevel(1);
			}
			if (userBaseData.uExp >= userBaseData.levelConfigData.endexp) {
				userBaseData.uExp = userBaseData.levelConfigData.endexp;
				isMaxExp = true;
			}
		}
		/**
		 *更新能量值
		 * @param value
		 * @param limit 是否限制能量上限
		 */
		public function changeEnergy(value:int, limit:Boolean = true):int
		{
			var temp:int = value;
			if(value > 0 && limit){
				temp = Math.max(0, GameConst.MAX_ENERGY - userBaseData.uEnergy);
				temp = Math.min(value, temp);
			}
			userBaseData.uEnergy += temp;
			if(userBaseData.uEnergy <= 0) sendCommand(CommandNameConst.COMMAND_NOT_ENOUGH_ENERGY, 0, 1);
			return temp;
		}
		/**
		 * 改变金币值
		 * @param	value
		 */
		public function changeCoins(value:int):void
		{
			userBaseData.uCoin += value;
		}
		/**
		 *改变庄园名称 
		 * @param s
		 * 
		 */		
		public function changeManorName(s:String):void
		{
			userBaseData.uManorName = s;
		}
		/**
		 *改变魅力值
		 * @param s
		 * 
		 */		
		public function changeMagic(value:int):void
		{
			userBaseData.uMagic += value;
		}
		
		public function levelHelpHandler():void
		{
			if(userBaseData.uLevel >= GameConst.MAX_LEVEL -1){
				userBaseData.uLevel = GameConst.MAX_LEVEL -1;
				userBaseData.levelConfigData = _configModel.analysisLevel[userBaseData.uLevel];
			}
		}
		
		/**
		 *用户基础数据 
		 * @return 
		 * 
		 */		
		public function get userBaseData():UserBaseData
		{
			return _userBaseData;
		}
	}
}